package com.example.tse.rbyair.widget.others;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tse.rbyair.R;

public class PlusLayout extends LinearLayout{
	private Context context;
	private View v;
	private TextView shopcart_reduce;
	private TextView shopcart_counttxt;
	private TextView shopcart_plus;
	private int count = 1;
	private int maxCount = 100;
	private OnPlusListener onPlusListener;
	private OnReduceListener onReduceListener;
	private OnNumChangeListener onNumChangeListener;
	private OnMaxListener onMaxListener;

	public PlusLayout(Context context){
		super(context);
		this.context = context;
		init();
	}
	
	public PlusLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}
	
	private void init(){
		v = LayoutInflater.from(context).inflate(R.layout.pluslay, this);
		shopcart_reduce = (TextView)v.findViewById(R.id.shopcart_reduce);
		shopcart_counttxt = (TextView)v.findViewById(R.id.shopcart_counttxt);
		shopcart_plus = (TextView)v.findViewById(R.id.shopcart_plus);
		
		shopcart_reduce.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(onNumChangeListener!=null){
					if(count-1<0) return;
					shopcart_counttxt.setText(""+(--count));
//				onReduceListener.onReduce(count);
					onNumChangeListener.onChanged(count);
					
				}
			}
		});
		
		shopcart_plus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(onNumChangeListener != null){
					if(count+1>maxCount){
						onMaxListener.onMaxHint();
						shopcart_counttxt.setText(""+maxCount);
						return;
					}else{
						shopcart_counttxt.setText(""+(++count));
//				onPlusListener.onPlus(count);
						onNumChangeListener.onChanged(count);
					}
					
				}
			}
		});
	}
	
	public int getCount(){
		return count;
	}
	
	public void setCount(int c){
		this.count = c;
		shopcart_counttxt.setText(""+count);
	}
	
	public void setMaxCount(int maxCount){
		this.maxCount = maxCount;
	}

	public interface OnPlusListener{
		void onPlus(int n);
	}
	
	public interface OnReduceListener{
		void onReduce(int n);
	}
	
	public interface OnNumChangeListener{
		void onChanged(int n);
	}
	
	public interface OnMaxListener{
		void onMaxHint();
	}
	
	public void setOnPlusListener(OnPlusListener onPlusListener){
		this.onPlusListener = onPlusListener;
	}
	
	public void setOnReduceListener(OnReduceListener onReduceListener){
		this.onReduceListener = onReduceListener;
	}
	
	public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener){
		this.onNumChangeListener = onNumChangeListener;
	}
	
	public void setOnMaxListener(OnMaxListener onMaxListener){
		this.onMaxListener = onMaxListener;
	}
	
}
